from PyQt5 import QtWidgets


class Setting(QtWidgets.QWidget):
    def __init__(self, name):
        super(Setting, self).__init__()
        self.name = name
        self.layout = QtWidgets.QVBoxLayout()

    def getValue(self):
        # Define in subclasses
        pass

    def _organizeWidgets(self, *args):
        for widget in args:
            self.layout.addWidget(widget)
        self.setLayout(self.layout)


class CheckboxSetting(Setting):
    def __init__(self, name, baseValue):
        super(self.__class__, self).__init__(name)
        self.entry = QtWidgets.QCheckBox(name)
        self.entry.setChecked(baseValue)
        self._organizeWidgets(self.entry)

    def getValue():
        return {self.name: self.entry.isChecked()}


class FilePathSetting(Setting):
    def __init__(self, name, basePath):
        super(self.__class__, self).__init__(name)
        self.label = QtWidgets.QLabel(name)
        self.entry = QtWidgets.QLineEdit(basePath)
        self.button = QtWidgets.QToolButton()
        self.button.clicked.connect(self.getPath)

        self.layout = QtWidgets.QHBoxLayout()
        self._organizeWidgets(self.label, self.entry, self.button)

    def getPath(self):
        path = QtWidgets.QFileDialog().getExistingDirectory()
        self.entry.insert(path)

    def getValue():
        return {self.name: self.entry.text()}


class SettingsWidget(QtWidgets.QWidget):
    # For now, just a test.
    def __init__(self):
        super(SettingsWidget, self).__init__()
        self.layout = QtWidgets.QVBoxLayout()
        self.layout.setSpacing(0)
        self.settings = []
        self.settings.append(CheckboxSetting('Update Games Automatically',
                             False))
        self.settings.append(FilePathSetting('Install Location', "/home"))
        self._organizeWidgets()

    def _organizeWidgets(self):
        for widget in self.settings:
            self.layout.addWidget(widget)
        self.layout.addStretch(1)
        self.setLayout(self.layout)

    def getValues(self):
        ret = {}
        for setting in self.settings:
            ret.update(setting.getValue())
        return ret

if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    window = SettingsWidget()
    window.setWindowTitle("SettingsTest")
    window.resize(500, 500)
    window.show()
    app.exec_()
